<template>
  <div class="login-connter">
    <van-nav-bar title="登录" class="page-nav-bar">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        v-model="user.mobile"
        :rules="userFormRulse.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        name="code"
        placeholder="请输入验证码"
        v-model="user.code"
        :rules="userFormRulse.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="1000 * 60"
            format="ss s"
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
          />
          <van-button
            v-else
            class="send-sms-btn"
            round
            size="small"
            type="primary"
            native-type="button"
            @click="onSendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-warp">
        <van-button round block type="info">提交</van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import {
  login, sendSms
} from '@/api/users'
export default {
  data () {
    return {
      // 登录用户数据
      user: {
        mobile: '13999999999',
        code: '246810'
      },
      // 用户登录表单验证规则
      userFormRulse: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3|5|7|8|]\d{9}$/, message: '手机号格式错误' }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      // 倒计时显示隐藏属性
      isCountDownShow: false
    }
  },
  methods: {
    // 发起登录请求
    async onSubmit () {
      const user = this.user
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 2000
      })
      try {
        const { data } = await login(user)
        this.$store.commit('setUser', data.data)
        console.log('登录成功', data)
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          console.log('手机验证码错误')
          this.$toast.fail('手机验证码错误')
        } else {
          console.log('登录失败，请稍后重试', err)
          this.$toast.fail('登录失败，请稍后重试')
        }
      }
    },
    // 发送验证码请求
    async onSendSms () {
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log(123)
      } catch (err) {
        return console.log(456)
      }
      this.isCountDownShow = true
      try {
        await sendSms(this.user.mobile)
        console.log('发送成功')
        this.$toast('发送成功')
      } catch (err) {
        if (err.response.status === 429) {
          this.isCountDownShow = false
          this.$toast('发送验证码操作频繁 请稍后重试')
        } else {
          this.$toast('发送失败,请稍后重试')
        }
      }
    }
  }
}
</script>
<style scoped lang="less">
.toutiao {
  font-size: 37px;
}
.send-sms-btn {
  color: #727272;
  width: 160px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px !important;
}
.login-btn-warp {
  padding: 53px 33px;
  .van-button {
    background-color: #6db4fb;
  }
}
</style>
